package com.ziyun.erp.web.modules.order.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.order.dto.*;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.entity.OrderRemarksEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderRemarksService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.production.operate.ErpOrderStateContext;
import com.ziyun.erp.modules.production.service.ErpOrderItemService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * erp订单信息表
 *
 * @author guizhao
 * @date 2018-07-27 11:19:18
 */
@RestController
@RequestMapping("order/erporder")
public class ErpOrderController {

    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private OrderRemarksService orderRemarksService;
    @Autowired
    private ErpOrderItemService erpOrderItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("supplierId", ErpOrderStateContext.ZIYUN_SELLER_ID);
        return erpOrderService.queryPageErpOrderList(params);
    }

    /**
     * 列表
     */
    @RequestMapping("/erpOrderlist")
    public R erpOrderlist(@RequestParam Map<String, Object> params){
        return erpOrderService.queryPage(params);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{orderNo}")
//    @RequiresPermissions("order:erporder:info")
    public R info(@PathVariable("orderNo") String orderNo){
        R re = R.ok();
        // 订单信息
        ErpOrderEntity erpOrder =  erpOrderService.selectByNo(orderNo);
        re.put("erpOrder", erpOrder);

        // 客服备注
        List<OrderRemarksEntity> remarkList = orderRemarksService.selectList(
                new EntityWrapper<OrderRemarksEntity>()
                        .eq("or_del", "0")
                        .eq("or_order_id", erpOrder.getOriginalOrderId())
        );
        re.put("remarkList", remarkList);

        // 商品列表
        List<ErpOrderItemEntity> erpOrderItemEntityList = erpOrderItemService.selectList(
                new EntityWrapper<ErpOrderItemEntity>()
                .eq("order_no",orderNo)
                .eq("item_del",0)
        );
        re.put("erpOrderItemEntityList", erpOrderItemEntityList);

        return re;
    }

    /**
     * 订单分配页面信息 ()
     * @param orderNo
     * @return
     */
    @RequestMapping("/allotInfo/{orderNo}")
    public R allotInfo(@PathVariable("orderNo") String orderNo){
        return erpOrderService.allotInfo(orderNo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:erporder:save")
    public R save(@RequestBody ErpOrderEntity erpOrder){
        erpOrderService.insert(erpOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:erporder:update")
    public R update(@RequestBody ErpOrderEntity erpOrder){
        ValidatorUtils.validateEntity(erpOrder);
        erpOrderService.updateById(erpOrder);
        
        return R.ok();
    }

    /**
     * 修改收货人信息
     */
    @RequestMapping("/orderReceiverUpdate")
    @RequiresPermissions("order:erporder:update")
    public R orderReceiverUpdate(@RequestBody ErpOrderEntity erpOrder){
        return erpOrderService.orderReceiverUpdate(erpOrder);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:erporder:delete")
    public R delete(@RequestBody String[] orderNos){
        erpOrderService.deleteBatchIds(Arrays.asList(orderNos));

        return R.ok();
    }

    /**
     * 确认付款
     */
    @RequestMapping("/receiveConfrim")
    public R receiveConfrim(@RequestBody FinanceReceiveMoneyDto financeReceiveMoneyDto){
        return erpOrderService.receiveConfrim(financeReceiveMoneyDto);
    }

    /**
     * 修改付款信息
     */
    @RequestMapping("/updateDialogForm")
    public R updateDialogForm(@RequestBody FinanceReceiveMoneyDto financeReceiveMoneyDto){
        SysUserEntity curentUser = UserUtils.getCurentUser();
        return erpOrderService.updateDialogForm(financeReceiveMoneyDto,curentUser);
    }

    /**
     * 确认付款备注修改
     */
    @RequestMapping("/receiveRemark")
    public R receiveRemark(@RequestParam("orderNo") String orderNo, @RequestParam("receiveMoneyRemark")String receiveMoneyRemark){
        return erpOrderService.receiveRemark(orderNo,receiveMoneyRemark);
    }

    /**
     * 财务订单计入成本统计
     */
    @RequestMapping("/costRecordConfirm")
    public R costRecordConfirm(@RequestParam("orderNo") String orderNo, @RequestParam("costRecord")Integer costRecord){
        return erpOrderService.costRecordConfirm(orderNo,costRecord);
    }

    /**
     * 订单分配
     * @param orderAllotDto
     * @return
     */
    @RequestMapping("/allotOrder")
    public R allotOrder(@RequestBody OrderAllotDto orderAllotDto){
        return erpOrderService.allotOrder(orderAllotDto);
    }

    /**
     * 订单商品分配
     * @param orderItemAllotDto
     * @return
     */
    @RequestMapping("/allotOrderItem")
    public R allotOrderItem(@RequestBody OrderItemAllotDto orderItemAllotDto){
        Long userId = UserUtils.getCurentUser().getUserId();
        orderItemAllotDto.setUserId(userId.intValue());
        return erpOrderService.allotOrderItem(orderItemAllotDto);
    }

    /**
     * 订单商品分配（阳逻工厂）
     * @param orderItemAllotDto
     * @return
     */
    @RequestMapping("/allotOrderItemYangluo")
    public R allotOrderItemYangluo(@RequestBody OrderItemAllotDto orderItemAllotDto){
        Long userId = UserUtils.getCurentUser().getUserId();
        orderItemAllotDto.setUserId(userId.intValue());
        orderItemAllotDto.setProducer(ErpOrderStateContext.YANGLUO_SUPP_ID);
        return erpOrderService.allotOrderItem(orderItemAllotDto);
    }

    /**
     * 设计商品分配
     * @return
     */
    @RequestMapping("/allotOrderItemDesign")
    public R allotOrderItemDesign(@RequestBody OrderItemAllotDto orderItemAllotDto){
        Long userId = UserUtils.getCurentUser().getUserId();
        orderItemAllotDto.setUserId(userId.intValue());
        return erpOrderService.allotOrderItemDesign(orderItemAllotDto);
    }

    /**
     * 订单工艺分配
     * @return
     */
    @RequestMapping("/allotOrderCrafts")
    public R allotOrderCrafts(@RequestBody OrderCraftsDto orderCraftsDto){
        return erpOrderService.allotOrderCrafts(orderCraftsDto);
    }

    /**
     * 删除分配信息
     * @param productionId
     * @param orderId
     * @return
     */
    @RequestMapping("/deleteAllotItem")
    public R deleteAllotItem(@RequestParam("productionId") Integer productionId, @RequestParam("orderId") String orderId){
        return erpOrderService.deleteAllotItem(productionId, orderId);
    }

    /**
     * 查询工厂信息（订单分配）
     * @param orderNo
     * @return
     */
    @RequestMapping("/orderAllotFindFactory")
    public R orderAllotFindFactory(@RequestParam("orderNo") String orderNo){
        return erpOrderService.orderAllotFindFactory(orderNo);
    }

    /**
     * 查询工厂信息（标品商品分配）
     * @param itemId
     * @param sellerType 1： 自营门店  2：第三方
     * @return
     */
    @RequestMapping("/orderItemAllotFindFactory")
    public R orderItemAllotFindFactory(@RequestParam("itemId") Integer itemId,@RequestParam("sellerType")Integer sellerType){
        return erpOrderService.orderItemAllotFindFactory(itemId, sellerType);
    }

    /**
     * 查询工厂信息（报价商品分配）
     * @param itemId
     * * @param sellerType 1： 自营门店  2：第三方
     * @return
     */
    @RequestMapping("/quoteOrderItemAllotFindFactory")
    public R quoteOrderItemAllotFindFactory(@RequestParam("itemId") Integer itemId,@RequestParam("sellerType")Integer sellerType){
        return erpOrderService.quoteOrderItemAllotFindFactory(itemId, sellerType);
    }

    /**
     * 查询工厂信息（工艺分配）
     * @param orderCraftQuoteDto
     * @return
     */
    @RequestMapping("/orderCraftAllotFindFactory")
    public R orderCraftAllotFindFactory(@RequestBody OrderCraftQuoteDto orderCraftQuoteDto){
        return erpOrderService.orderCraftAllotFindFactory(orderCraftQuoteDto);
    }

    /**
     * 商品工序列表
     * @param itemId
     * @return
     */
    @RequestMapping("/orderCraftsInfo")
    public R orderCraftsInfo(@RequestParam("itemId") Integer itemId){
        return erpOrderService.orderCraftsInfo(itemId);
    }

    /**
     * 订单标记
     * @return
     */
    @RequestMapping("/orderTagChange")
    public R orderTagChange(@RequestParam("orderNo") String orderNo, @RequestParam("orderTag") Integer orderTag){
        return erpOrderService.orderTagChange(orderNo, orderTag);
    }

    /**
     * 添加外协商
     * @param orderSupplierDto
     * @return
     */
    @RequestMapping("/suppSave")
    public R suppSave(@RequestBody OrderSupplierDto orderSupplierDto){
        Long userId = UserUtils.getCurentUser().getUserId();
        return erpOrderService.suppSave(orderSupplierDto, userId);
    }

    /**
     * 外协成本录入
     * @param orderSupplierDto
     * @return
     */
    @RequiresPermissions("order:erporder:costsave")
    @RequestMapping("/costSave")
    public R costSave(@RequestBody OrderSupplierDto orderSupplierDto){
        Long userId = UserUtils.getCurentUser().getUserId();
        return erpOrderService.costSave(orderSupplierDto, userId);
    }

    /**
     * 订单初始化
     * @param orderId
     * @return
     */
    @RequestMapping("/orderInit")
    public R orderInit(@RequestParam("orderId") String orderId){
        Long userId = UserUtils.getCurentUser().getUserId();
        return erpOrderService.orderInit(orderId, userId);
    }

    /**
     * 设计商品初始化
     * @param eoItemId
     * @return
     */
    @RequestMapping("/resetOrderItemDesign")
    public R resetOrderItemDesign(@RequestParam("eoItemId") Integer eoItemId){
        return erpOrderService.resetOrderItemDesign(eoItemId);
    }

    /**
     * 订单完成计划任务（暂用）
     * @return
     */
    @RequestMapping("/doOrderFinisJob")
    public R doOrderFinishJob(){
        try {
            return erpOrderService.doOrderFinishJob();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @RequestMapping("/testSellerCashBack")
    public void testSellerCashBack(){
        List<String> list = new ArrayList<>();
        list.add("180614104747094");
        list.add("181014142089005");

        erpOrderService.sellerCashBack(list);
    }

}
